﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Pseudogenerator.Common.Generators;
using Pseudogenerator.Common.ViewModels.Core;

namespace Pseudogenerator.Common.ViewModels
{
    public class MultiplicativeCongruentialViewModel
        : CongruentialViewModel<MultiplicativeCongruential>
    {

        public MultiplicativeCongruentialViewModel()
        {
            this.ModeloActual = new MultiplicativeCongruential();
        }

        #region ModelFields

        private bool AddFirst = false;
        public string First
        {
            get
            {
                return AddFirst ? "+" : "-";
            }
        }
        public string Last
        {
            get
            {
                return AddFirst ? "-" : "+";
            }
        }

        public ICommand InvertModeCommand
        {
            get
            {
                return new RelayCommand("Invertir signo", a =>
                {
                    AddFirst = !AddFirst;
                    OnPropertyChanged("First");
                    OnPropertyChanged("Last");
                    OnPropertyChanged("t");
                    OnPropertyChanged("a");
                }
                );
            }
        }

        public float t
        {
            get
            {
                float r = (a - (3f * (AddFirst ? 1 : -1))) / 8f;
                return r;
            }
            set
            {
                a = (uint)((8 * value) + (3 * (AddFirst ? 1 : -1))); 
            }
        }

        #endregion

        public override string Title
        {
            get { return "Congruencial multiplicativo"; }
        }
        
        protected override void AddPreload()
        {
            var actual = Preloads.FirstOrDefault(i => i.a == a && i.x0 == x0 && i.m == m);
            if (actual == null)
            {
                Preloads.Add(new MultiplicativeCongruential() { a = a, x0 = x0, m = m });
            }
        }

        public override IEnumerable<string> Errors
        {
            get
            {
                if (ModeloActual.a <= 0)
                    yield return "Los parametros deben ser mayor que 0";
                if (ModeloActual.m <= ModeloActual.x0 ||
                    ModeloActual.m <= ModeloActual.a)
                    yield return "El Modulo ('m') debe ser mayor que los demas parametros";
            }
        }

        public override IEnumerable<string> Recommendations
        {
            get
            {
                if(SonPrimosRelativos(ModeloActual.x0,ModeloActual.m))
                    yield return "Se recomienda que 'Xo' sea relativamente primo a m.";

                if(t.ToString().Contains(",") || t.ToString().Contains("."))
                    yield return "Se recomienda que 't' sea un número entero.";
            }
        }

        private MultiplicativeCongruential _ModeloActual = null;
        public override MultiplicativeCongruential ModeloActual
        {
            get
            {
                if (_ModeloActual == null)
                    _ModeloActual = new MultiplicativeCongruential();

                return _ModeloActual;
            }
            set
            {
                if (value == null) return;
                this.a = value.a;
                this.x0 = value.x0;
                this.m = value.m;
                ClearCommand.Execute(null);
            }
        }

    }
}
